﻿package frEngine.primitives
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.utils.*;
    import flash.geom.*;
    import frEngine.core.*;
    import frEngine.render.*;
    import frEngine.render.layer.*;
    import frEngine.shader.filters.*;

    public class FrPlane extends Mesh3D
    {
        private var _width:Number;
        private var _height:Number;
        private var _segments:int;
        private var _axis:String;
        public var offsetMatrix:Matrix3D;
        public var faceCamera:Boolean;
        public var faceCameraAxis:Vector3D;
        private static var _surfXY:FrSurface3D;
        private static var _surfXYNeg:FrSurface3D;
        private static var _surfXZ:FrSurface3D;
        private static var _surfXZNeg:FrSurface3D;
        private static var _surfYZ:FrSurface3D;
        private static var _surfYZNeg:FrSurface3D;

        public function FrPlane(param1:String)
        {
            this.offsetMatrix = new Matrix3D();
            super(param1, true, null);
            this.setLayer(Layer3DManager.AlphaLayer0);
            this.render = FrPlaneRender.instance;
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.faceCamera = false;
            this.faceCameraAxis = null;
            this.offsetMatrix.identity();
            super.dispose(param1);
            return;
        }// end function

        public function reinit(param1:RenderList, param2:Number, param3:Number, param4, param5:Boolean, param6:Vector3D, param7:String = "+xy") : void
        {
            if (!param4)
            {
                param4 = 13421772;
            }
            this.render = FrPlaneRender.instance;
            this.renderList = param1;
            this._axis = param7;
            this._segments = 1;
            this._height = param3;
            this._width = param2;
            this.faceCamera = param5;
            this.faceCameraAxis = param6;
            this.offsetMatrix.identity();
            switch(param7)
            {
                case "+xy":
                {
                    var _loc_8:* = createPlane(1, param7);
                    _surfXY = createPlane(1, param7);
                    this.setSurface(0, _surfXY);
                    this.offsetMatrix.appendScale(param2, param3, 1);
                    break;
                }
                case "-xy":
                {
                    var _loc_8:* = createPlane(1, param7);
                    _surfXYNeg = createPlane(1, param7);
                    this.setSurface(0, _surfXYNeg);
                    this.offsetMatrix.appendScale(param2, param3, 1);
                    break;
                }
                case "+xz":
                {
                    var _loc_8:* = createPlane(1, param7);
                    _surfXZ = createPlane(1, param7);
                    this.setSurface(0, _surfXZ);
                    this.offsetMatrix.appendScale(param2, 1, param3);
                    break;
                }
                case "-xz":
                {
                    var _loc_8:* = createPlane(1, param7);
                    _surfXZNeg = createPlane(1, param7);
                    this.setSurface(0, _surfXZNeg);
                    this.offsetMatrix.appendScale(param2, 1, param3);
                    break;
                }
                case "+yz":
                {
                    var _loc_8:* = createPlane(1, param7);
                    _surfYZ = createPlane(1, param7);
                    this.setSurface(0, _surfYZ);
                    this.offsetMatrix.appendScale(1, param2, param3);
                    break;
                }
                case "-yz":
                {
                    var _loc_8:* = createPlane(1, param7);
                    _surfYZNeg = createPlane(1, param7);
                    this.setSurface(0, _surfYZNeg);
                    this.offsetMatrix.appendScale(1, param2, param3);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.setMaterial(param4, Texture3D.MIP_NONE, "frPlane");
            this.setMateiralBlendMode(EBlendType.BLEND_LIGHT);
            this.material.materialParams.depthWrite = false;
            this.material.materialParams.twoSided = true;
            this.updateBounds();
            return;
        }// end function

        private function updateBounds() : void
        {
            var _loc_1:* = this.width * 0.5;
            var _loc_2:* = this.height * 0.5;
            var _loc_3:* = new Boundings3D();
            if (this._axis.indexOf("xy") != -1)
            {
                _loc_3.max.setTo(_loc_1, _loc_2, 0);
                _loc_3.min.setTo(-_loc_1, -_loc_2, 0);
            }
            else if (this._axis.indexOf("xz") != -1)
            {
                _loc_3.max.setTo(_loc_1, 0, _loc_2);
                _loc_3.min.setTo(-_loc_1, 0, -_loc_2);
            }
            else if (this._axis.indexOf("yz") != -1)
            {
                _loc_3.max.setTo(0, _loc_1, _loc_2);
                _loc_3.min.setTo(0, -_loc_1, -_loc_2);
            }
            _loc_3.length = _loc_3.max.subtract(_loc_3.min);
            _loc_3.radius = Vector3D.distance(_loc_3.center, _loc_3.max);
            this.bounds = _loc_3;
            return;
        }// end function

        public function get axis() : String
        {
            return this._axis;
        }// end function

        public function get segments() : int
        {
            return this._segments;
        }// end function

        public function get height() : Number
        {
            return this._height;
        }// end function

        public function get width() : Number
        {
            return this._width;
        }// end function

        private static function createPlane(param1:int, param2:String) : FrSurface3D
        {
            var _loc_3:int = 0;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:* = new FrSurface3D("plane");
            _loc_8.addVertexData(FilterName_ID.POSITION_ID, 3, false, null);
            _loc_8.addVertexData(FilterName_ID.NORMAL_ID, 3, false, null);
            _loc_8.addVertexData(FilterName_ID.UV_ID, 2, false, null);
            var _loc_9:* = _loc_8.getVertexBufferByNameId(FilterName_ID.POSITION_ID).vertexVector;
            var _loc_10:* = new Matrix3D();
            if (param2 == "+xy")
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(0, 0, -1));
            }
            else if (param2 == "-xy")
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(0, 0, 1));
            }
            else if (param2 == "+xz")
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(0, 1, 0));
            }
            else if (param2 == "-xz")
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(0, -1, 0));
            }
            else if (param2 == "+yz")
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(1, 0, 0));
            }
            else if (param2 == "-yz")
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(-1, 0, 0));
            }
            else
            {
                Matrix3DUtils.setOrientation(_loc_10, new Vector3D(0, 0, -1));
            }
            var _loc_11:* = _loc_10.rawData;
            var _loc_12:* = Matrix3DUtils.getDir(_loc_10);
            _loc_3 = 0;
            _loc_5 = 0;
            while (_loc_5 <= param1)
            {
                
                _loc_4 = 0;
                while (_loc_4 <= param1)
                {
                    
                    _loc_6 = _loc_4 / param1 - 0.5;
                    _loc_7 = _loc_5 / param1 - 0.5;
                    _loc_9.push(_loc_6 * _loc_11[0] + _loc_7 * _loc_11[4] + _loc_11[12], _loc_6 * _loc_11[1] + _loc_7 * _loc_11[5] + _loc_11[13], _loc_6 * _loc_11[2] + _loc_7 * _loc_11[6] + _loc_11[14], _loc_12.x, _loc_12.y, _loc_12.z, _loc_4 / param1, 1 - _loc_5 / param1);
                    _loc_3++;
                    _loc_4 = _loc_4 + 1;
                }
                _loc_5 = _loc_5 + 1;
            }
            _loc_3 = 0;
            _loc_5 = 0;
            var _loc_13:* = _loc_8.indexVector;
            while (_loc_5 < param1)
            {
                
                _loc_4 = 0;
                while (_loc_4 < param1)
                {
                    
                    var _loc_14:* = _loc_3 + 1;
                    _loc_13[_loc_14] = (_loc_4 + 1) + _loc_5 * (param1 + 1);
                    var _loc_15:* = _loc_3 + 1;
                    _loc_13[_loc_15] = (_loc_4 + 1) + (_loc_5 + 1) * (param1 + 1);
                    var _loc_16:* = _loc_3 + 1;
                    _loc_13[_loc_16] = _loc_4 + (_loc_5 + 1) * (param1 + 1);
                    var _loc_17:* = _loc_3 + 1;
                    _loc_13[_loc_17] = _loc_4 + _loc_5 * (param1 + 1);
                    var _loc_18:* = _loc_3 + 1;
                    _loc_13[_loc_18] = (_loc_4 + 1) + _loc_5 * (param1 + 1);
                    var _loc_19:* = _loc_3 + 1;
                    _loc_13[_loc_19] = _loc_4 + (_loc_5 + 1) * (param1 + 1);
                    _loc_4 = _loc_4 + 1;
                }
                _loc_5 = _loc_5 + 1;
            }
            return _loc_8;
        }// end function

    }
}
